/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.administrator.deletemanager;

import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCDeleteManager;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 *
 * @author Administrator
 */
public class DeleteManagerView extends JFrame{  
    private final static String userFieldParse = "mmmmmmmmmm";

    private final static int EXIT_TYPE = JFrame.DO_NOTHING_ON_CLOSE;
    
    private DeleteManagerModel model;
    
    private JLabel usernameLabel;      
    private JTextField usernameField;      
    private JButton deleteButton;
    private JButton cancelButton;
    
    private static DeleteManagerView instance;

    protected DeleteManagerView() {
        this(new DeleteManagerModel());
    }

    protected DeleteManagerView(DeleteManagerModel model) {
        this.model = model;
        createFrame();
    }
    /**
     * get the current instance, if the instance is null create a new one
     * @return
     * instance
     */
    public static DeleteManagerView getInstance() {
        if (instance == null){
            instance = new DeleteManagerView();
        }
        return instance;
    }
    
    /**
     * Shows the gui and sets running to true
     */
    public void showGUI(){

        setVisible(true);
    }
    /**
     * Hides the gui and sets running to false
     */
    public void hideGUI(){
        setVisible(false);
    }

    /**
     * Function to create the delete frame
     * Has fields for username and password
     */
    public void createFrame(){
        GridLayout grid = new GridLayout(0,2);
        setLayout(grid);
        
        //create buttons, labels etc
        usernameLabel = new JLabel("Enter Managers Username:");
        usernameLabel.setHorizontalAlignment(SwingConstants.LEFT);        

        usernameField = new JTextField();        
        setFieldWidth(usernameField,userFieldParse);
        
        JPanel deleteButtonPanel = new JPanel(new FlowLayout());
        
        deleteButton = new JButton("Delete");       
        deleteButtonPanel.add(deleteButton);
        
        cancelButton = new JButton("Cancel");
        deleteButtonPanel.add(cancelButton);
        
        //first row username lable and field
        add(usernameLabel); add(usernameField);
        //second row blank pannel and delete button panel
        add(new JPanel()); add(deleteButtonPanel);
        
        cancelButton.addActionListener(new CancelButtonAL());
        deleteButton.addActionListener(new UC2AL(new UCDeleteManager(model, this)));
        
        //display        
        setTitle("Delete User");
        setDefaultCloseOperation(EXIT_TYPE);
        setAlwaysOnTop(true);
        setResizable(false);
        pack();
        setLocationRelativeTo(null); //center frame
    }
    
    private class CancelButtonAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            hideGUI();
            canceledMessageBox();
        }
    }
    /**
     * Sets the size of the field to the size generated by the string length then clears the field
     * @param field
     * Field to be set
     * @param length
     * String that is the length that is required
     */
    public void setFieldWidth(JTextField field, String length){
        field.setText(length);
        field.setPreferredSize(field.getPreferredSize());
        field.setText("");
    }
    /**
     * Create a message dialog with the same inputs as the showmessagedialog function.
     * Used for ease when creating messages
     * @param parentcomponent
     * @param message
     * Message to be displayed
     * @param title
     * Title of the dialog
     * @param messageType
     * Type of message
     */
    public void message(Component parentcomponent,
            Object message,
            String title,
            int messageType ){            
        JOptionPane.showMessageDialog(parentcomponent,message,title,messageType);
    }
    /**
     * Create an invaild user/pass message box
     */
    public void usernameErrorMessageBox(){
        message(this,"Given Manager account doesnt exist",
        "Invalid Manager",
        JOptionPane.ERROR_MESSAGE);
    }
    /**
     * Create a message box to give a manager not manager message
     */
    public void notManagerMessageBox(){
        message(this,"The given username is not a Manager. User can only delete themselves and " +
                "Administrators cannot be deleted via this program",
        "System privilage error",
        JOptionPane.ERROR_MESSAGE);
    }
    /**
     * Create a success message box
     */
    public void successMessageBox(){
        message(this,"Account was successfully Deleted",
        "Account Deleted",
        JOptionPane.INFORMATION_MESSAGE);
    }
    /**
     * Create a cancel message box
     */
    public void canceledMessageBox(){
        message(this,"Delete has been canceled",
        "Canceled",
        JOptionPane.WARNING_MESSAGE);
    }
    /**
     * Get the username from the field text
     * @return
     * Username as a string
     */
    public String getUsernameField(){return usernameField.getText();}
    /**
     * Clear the username field
     */
    public void clearUsernameField(){
        usernameField.setText("");
    }
    
    
}
